//https://cdn.bootcdn.net/ajax/libs/FileSaver.js/2.0.5/FileSaver.min.js

export const downloadText = (() => {
  let saveAs: (blob: Blob, filename: string) => void
  return async (text: string, filename: string) => {
    if (!saveAs) {
      saveAs = await new Promise((resolve, reject) => {
        const script = document.createElement('script')
        script.setAttribute('src', 'https://cdn.bootcdn.net/ajax/libs/FileSaver.js/2.0.5/FileSaver.min.js')
        script.onload = () => resolve((window as any).saveAs)
        script.onerror = reject
        document.body.appendChild(script)
      })
    }
    saveAs(new Blob([text]), filename)
  }
})();
